var express = require('express');
var router = express.Router();
const connection = require('../settings/connect')
const pool = require('../settings/pool')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/get', function (req, res, next) {
  connection.connect()

  var query = connection.query('select * from user', function (err, rows, fields) {
    if(err) throw err
    res.json(rows)
  })

  connection.end()
})

router.get('/get2', function (req, res, next) {
  var query = pool.query('select * from user', function (err, rows, fields) {
    if (err) throw err
    res.json(rows)
  })
})

// get: req.query, get: req.body
router.get('/insert', function (req, res, next) {
  connection.connect()
  var username = req.query.username
  var password = req.query.password
  var age = req.query.age
  //var sql = "insert into user('username', 'password', 'age') values ('" + username + "', '"+ password +"', " + age + ")"
  var sql = "insert into user (username, password, age) values ('Alax', '123', 4)";
  console.log(sql)
  var query = connection.query(sql, function (err, rows, fields) {
      if(err) throw err
      res.json({
        success: true,
        msg: "插入成功",
        data: req.query
      })
  })

  connection.end()
})

module.exports = router;
